<?php

namespace addons\booking\model;

use think\Model;
use think\Db;

class WorkerAuth extends Model
{


    // 表名
    protected $name = 'worker_auth';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    //提交实名认证
    public static function save_auth($user_id=0,$real_image='',$card_front='',$card_back='')
    {
        $worker_info = Worker::where('user_id',$user_id)->find();
        if(!$worker_info) return ['status'=>-1,'msg'=>'您还不是保洁'];

        $data['real_image'] = $real_image;
        $data['card_front'] = $card_front;
        $data['card_back'] = $card_back;
        $data['create_time'] = time();
        $data['examine_time'] = time();
        $data['examine_content'] = '';
        $data['status'] = 0;
        $data['user_id'] = $user_id;
        $data['worker_id'] = $worker_info['id'];

        $auth = self::where('worker_id',$worker_info['id'])->find();
        Db::startTrans();
        try {
            if($auth){
                //修改
                self::where('worker_id',$worker_info['id'])->update($data);
            } else {
                //新增
                self::insert($data);
            }

            //更新保洁实名认证状态
            Worker::where('id',$worker_info['id'])->update(['is_auth'=>0]);
            return ['status'=>1,'msg'=>'提交成功'];
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['status'=>-1,'msg'=>$e->getMessage()];
        }


    }

}
